#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=3e3+100;
typedef int LL;
typedef long long Rll;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
struct Edge{
   LL to;Rll val;
};
vector<Edge>g[maxn];
Rll dp[maxn][maxn];
LL siz[maxn];
LL n,m;
void dfs(LL u,LL fa){
     siz[u]=1;
     dp[u][0]=dp[u][1]=0;
     for(LL i=0;i<g[u].size();i++){
         LL v=g[u][i].to;Rll w=g[u][i].val;
         if(v==fa) continue;
         dfs(v,u);
         for(LL j=min(m,siz[u]);j>=0;j--){
             for(LL k=min(siz[v],m-j);k>=0;k--){
                 Rll ans=w*( (n-m-siz[v]+k)*(siz[v]-k)+(m-k)*k);
                 dp[u][j+k]=max(dp[u][j+k],dp[u][j]+dp[v][k]+ans );
             }
         }
         siz[u]+=siz[v];
     }
}
int main(void){
   n=read();m=read();
   for(LL i=1;i<n;i++){
       LL u,v,w;u=read();v=read();w=read();
       g[u].push_back({v,w});
       g[v].push_back({u,w});
   }
   memset(dp,-0x3f,sizeof(dp));
   dfs(1,0);
   printf("%lld\n",dp[1][m]);
   return 0;
}
